package com.whp.openapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.dao.UserTokenMapper;
import com.whp.openapi.entity.UserToken;
import com.whp.openapi.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author whp
 * @date 2021/11/27
 */
@Service
public class UserTokenServiceImpl implements UserTokenService {
    
    @Autowired
    private UserTokenMapper userTokenMapper;

    @Override
    public ResultTable table(Integer page, Integer limit, String accessToken) {
        PageHelper.startPage(page, limit);
        List<UserToken> userTokens = userTokenMapper.getTable(accessToken);
        PageInfo pi = new PageInfo(userTokens);
        return new ResultTable(0, "msg", (int) pi.getTotal(), pi.getList());
    }

    @Override
    public ResponseResult delUserToken(Integer[] ids) {
        int i = userTokenMapper.delUserToken(ids);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("删除失败");
    }

    @Override
    public ResponseResult addUserToken(UserToken userToken) {
        if (userToken != null) {
            int i = userTokenMapper.insertSelective(userToken);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("添加失败");
    }

    @Override
    public ResponseResult updateUserToken(UserToken userToken) {
        if (userToken != null) {
            int i = userTokenMapper.updateByPrimaryKeySelective(userToken);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("修改失败");
    }
}
